var ws, uid;
document.addEventListener('plusready',function(){
	showWating();
	uid = plus.storage.getItem('uid');
	ws = plus.webview.currentWebview();
	$('.addNewfamily').on('tap', function(){
		goNewPage('me-family-add.html',{page:ws.id});
	})
	$('.search').on('tap', function(){
		goNewPage('me-family-search-list.html');
	})
	console.log(apiRoot+"?m=home&c=member&a=familylist&uid="+uid);
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=member&a=familylist&uid="+uid,
		success :function(data){
			plus.nativeUI.closeWaiting();
			plus.storage.setItem('family',data);
			var data = $.parseJSON(data);
//			console.log(JSON.stringify(data));
			var list = '';
			$.each(data, function(k,v) {
//				console.log(JSON.stringify(v));
				plus.nativeUI.closeWaiting();
				list += '<div class="mui-table-view mui-table-view-cell bbs-ico lifamily'+v.aid+'">'+
							'<div class="overflow-h mui-slider-handle goinfo" fid="'+v.aid+'">'+
								'<i><img src="'+getAvatar(v.photo)+'"/></i>'+
								'<div>'+
									'<li>'+
										'<span>'+v.name+'</span>'+
										'<span class="mui-h6 mui-pull-right">'+((v.sex == 0)?'男':'女')+'</span>'+
									'</li>'+
									'<li class="mui-h6 ">'+
										'<span>手机:'+v.phone+'</span>'+
										'<span class="mui-pull-right">'+v.age+'岁</span>'+
									'</li>'+
								'</div>'+
							'</div>'+
							'<div class="mui-slider-right mui-disabled" onclick="delfamily(' + v.aid + ')">'+
								'<a class="mui-btn mui-btn-red">删除</a>'+
							'</div>'+
						'</div>';
			});
			$('.familylist').html(list);
			$('.goinfo').on('tap', function(){
				goNewPage('me-family-bianji.html',{aid:$(this).attr('fid'),page:ws.id})
			})
		},
		error :function(e){
			console.log(JSON.stringify(e));
			errortoast();
			var fdata = plus.storage.getItem('family');
			var data = $.parseJSON(fdata);
//			console.log(JSON.stringify(data));
			var list = '';
			$.each(data, function(k,v) {
//				console.log(JSON.stringify(v));
				plus.nativeUI.closeWaiting();
				list += '<div class="mui-table-view mui-table-view-cell bbs-ico lifamily'+v.aid+'">'+
							'<div class="overflow-h mui-slider-handle goinfo" fid="'+v.aid+'">'+
								'<i><img src="'+getAvatar(v.photo)+'"/></i>'+
								'<div>'+
									'<li>'+
										'<span>'+v.name+'</span>'+
										'<span class="mui-h6 mui-pull-right">'+((v.sex == 0)?'男':'女')+'</span>'+
									'</li>'+
									'<li class="mui-h6 ">'+
										'<span>手机:'+v.phone+'</span>'+
										'<span class="mui-pull-right">'+v.age+'岁</span>'+
									'</li>'+
								'</div>'+
							'</div>'+
							'<div class="mui-slider-right mui-disabled" onclick="delfamily(' + v.aid + ')">'+
								'<a class="mui-btn mui-btn-red">删除</a>'+
							'</div>'+
						'</div>';
			});
			$('.familylist').html(list);
			$('.goinfo').on('tap', function(){
				goNewPage('me-family-bianji.html',{aid:$(this).attr('fid'),page:ws.id})
			})
		}
	});
})
//删除家庭成员
function delfamily(id){
	console.log(id);
	showWating();
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=member&a=delfamily",
		data :{
			id :id
		},
		dataType :'json',
		success :function(data){
			plus.nativeUI.closeWaiting();
			console.log(JSON.stringify(data));
			if(data > 0){
				toast('删除成功');  
				$('.lifamily'+id).remove();
			}
		},
		error :function(e){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
}